<?php

/*
 * taux/index.phtml
 * Page d'accueil de la gestion des taux horaires
 */
?>
<?php

$title = "Variation des taux horaires";
$this->headTitle($title);
?>

<div class='content-head'>
    <h3>
	<?php echo $title; ?>	
    </h3>
    
    <div class="toolbar">        
        <button id='btn-taux_load-list-process' class="btn btn-success ">Historique des traitements</button>
        <button id='btn-taux_load-main-process' class="btn btn-inverse ">Revenir à la liste des postes</button>
    </div>
    
</div>

<div class='content-body manage-process-taux'>
    <div class="add-toggle">
	<table class="table table-striped list-table">
           <thead>
                <td width='27%'>Taux horaire</td>
                <td width='27%'>Nouvelle valeur</td>
                <td width='27%'>Date d' application</td>
                <td width='19%'> </td>
           </thead>
	    <tbody>
		<tr>
		    <td>
			<a href="#" id="newProcTaux" data-store="taux" data-type="select" data-title='Choix du taux'></a>
		    </td>
                    <td>
			<a href="#" id="newProcTauxNew"  data-store="taux-new" data-type="text" data-title='Nouvelle valeur du taux'></a>
		    </td>
		    <td>
			<a href="#" id="newProcTauxDate"  data-store="taux-date" data-type="combodate"  data-title="Date d'application"  data-format= "DD/MM/YYYY" datatemplate= 'D / MMMM / YYYY' ></a>
		    </td>
                    
		    <td>
			<button id="newProcTauxAdd" class="btn btn-success">Ajouter</button>
		    </td>
		</tr>
	    </tbody>
	</table>
	<form id="formStore" action="#" method="post" action=''>
	    <input type="hidden" id="store-taux" name="taux" value="">
            <input type="hidden" id="store-taux-new" name="nouveau_taux" value="">
            <input type="hidden" id="store-taux-date" name="date_application" value="">            
	</form>
    </div>

    <div id='content-data' >
	<!-- AJAX LOADING -->
    </div>
    <div id="content-data_list-process" >  </div>
</div>

<!-- Script -->
<script type='text/javascript'>
    
    
    var JsTaux = (function(){
        var isEmpty = function(selector){
                return $.trim($(selector).val()) === '';
            },
            isFloat = function(val){
                v = $.trim(val);
                patFloat = new RegExp(/^[0-9]*[.]{0,1}[0-9]+$/);

                return patFloat.test(v);                     
            }
            ;
            
        return {
            init : function(){
                var self = this;
                
                JsTaux.refresh();                
                JsTaux.initAdding();
                JsTaux.general();
            },
            refresh : function(paramCall){ 
                var defautsParam =  {
                                "onglet" : '',
                                "callback" : null
                            };
                param = $.extend(defautsParam, paramCall);
                
                ajax = $.ajax({
                    type : "post",
                    url  : "<?php echo $this->url('RHX/default', array('controller'	 => 'taux', 'action'	 => 'load')) ?>",
                    data : "onglet="+ param.onglet
                    });
                ajax.success(function(data){
                    $('#content-data').html(data);
                });
                ajax.error(function(){
                    $("#content-data").html("<div class='rh-error'>Contenu Impossible à charger</div>");
                });
            },
            initAdding : function(){
                var store_data = function(idStore, v) {
                        var v_trimed = $.trim(v),  
                            selector = '#store-'+ idStore;
                        $(selector).val(v_trimed);                    
                    },
                    add_handler = function(){
                        if(isEmpty('#store-taux')){
                            alert("Vous devez selectionner un taux");
                            return false;
                        }
                        if(isEmpty('#store-taux-new')){
                            alert('Vous devez fournir la valeur du nouveau taux');
                            return false;
                        }
                        if(isEmpty('#store-taux-date')){
                            alert("Vous devez selectionner une date d'application");
                            return false;
                        }
                                
                        isValid = true;
                        if(isValid){
                            _data = $('form#formStore').serialize();
                            addAjax = $.ajax({
                                type : 'post',
                                url : '<?php echo $this->url('RHX/default', array('controller'	 => 'taux', 'action'	 => 'addTaux')) ?>',
                                data : _data                                
                            });
                            addAjax.done (function(data){ 
                                    window.location.reload();
                                });
                            addAjax.fail (function(data){
                                showError('Erreur Serveur');
                            });
                        }                        
                    };
                
              
                // init editable
                $('#newProcTaux').editable({
                    //source: [{value: 1, text : '124'}, {value: 2, text : '235'}],
                    source :  "<?php echo $this->url('RHX/default', array('controller'	 => 'taux', 'action'	 => 'jsonTauxList')) ?>",
                    validate :function(value){                        
                        store_data( $(this).data('store'), value); 
                    }
                });
                $('#newProcTauxNew').editable({
                    validate :function(value){
                        if(!isFloat(value))
                            return 'Donnée invalide';
                        store_data( $(this).data('store'), value); 
                    }
                });
                $('#newProcTauxDate').editable({
                     validate :function(value){
                        date_apply = value.year() + '-' +  (parseInt(value.month()) + 1)+ '-'+ value.date()
                        store_data( $(this).data('store'), date_apply); 
                    }
                });
                // init btn adding
                $('#newProcTauxAdd').on('click', add_handler);
                
            }, 
            general : function (){                
                $('#btn-taux_load-list-process').on('click', JsTaux.loadListProcess);
                $('#btn-taux_load-main-process').on('click', function(){
                    window.location.reload();
                });
            },
            loadListProcess : function(){
                var dataHTML = null,
                        toggleContentData = function(dataHTML){
                        //toggle button                    
                        $('#btn-taux_load-list-process').css({'display': 'none'});
                        $('#btn-taux_load-main-process').css({'display': 'block'});

                        //toggle content
                        $('#content-data').hide('normal', function(){
                            $('#content-data_list-process').html(dataHTML).show();                    
                        });

                    };
                
                ajax = $.ajax({
                        type : "post",
                        url  : "<?php echo $this->url('RHX/default', array('controller'	 => 'taux', 'action'	 => 'loadTaitement')) ?>"                            
                        });
                ajax.success(function(result){
                    dataHTML = result;
                    toggleContentData(dataHTML);
                });

            }
        } 
    } )();
    
    $(document).bind('ready', JsTaux.init);
</script>
